package com.crm.dao.saling;

import com.crm.pojo.Refund;
import com.crm.pojo.RefundDetail;
import com.crm.vo.RefundDetailProductVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/10
 */
public interface RefundDetailsDao extends CrudRepository<RefundDetail, Long> {
    @Query("from RefundDetail  r where r.refundDetailsIsDel='否'")
    public List<RefundDetail> showRefundDetails(Pageable pageable);

    @Query("from RefundDetail  r where r.refundDetailsIsDel='否' and r.refund=?1")
    public List<RefundDetail> showRefundDetailsByRefund(Refund refund);

    @Query("update RefundDetail  r set r.refundDetailsIsDel='是' where r.refund=?1")
    @Modifying
    public void deleteRefundDetailByRefund(Refund refund);

    @Query("from RefundDetail rd where rd.refundDetailsIsDel='否' and rd.refund.refundId=?1")
    public List<RefundDetail> queryAllByid(Long id);

    @Query(nativeQuery = true, value = "select count(*) from REFUND_DETAILS rd where rd.REFUND_DETAILS_IS_DEL='否' and rd.REFUND_ID=?1")
    public Long queryCountIn(Long id);

    @Query(nativeQuery = true, value = "select * from REFUND_DETAILS rd where rd.REFUND_DETAILS_IS_DEL='否' and rd.REFUND_ID=?1")
    public List<RefundDetail> queryAllByrid(Long id);

    @Query(nativeQuery = true, value = "select sum(rd.REFUND_DETAILS_AMOUNT) from REFUND_DETAILS rd join PRODUCTION_DETAILS pd on rd.FK_REFUND_PRODUCT_ID=pd.PID where pd.PRODUCTION_DETAILS_IS_DEL='未删除' and rd.FK_REFUND_PRODUCT_ID=?1 and rd.FK_REFUND_ID=1")
    public Long queryCountByproid(Long proid);

    /**
     * 获取退货详单数据列表
     *
     * @param pageable
     * @return
     */
    @Query("select new com.crm.vo.RefundDetailProductVO(refundDetail,product) from RefundDetail  refundDetail ,ProductionDetail  product where product.pid=refundDetail.fkRefundProductId and refundDetail.refundDetailsIsDel='否'")
    public List<RefundDetailProductVO> getRefundDetailProductVO(Pageable pageable);

    /**
     * 这注释老子不写
     *
     * @return
     */
    @Query("select count(r) from RefundDetail  r where r.refundDetailsIsDel='否'")
    public Long getRefundDetailCount();

    /**
     * 获取对应的发货详单
     *
     * @param refundId
     * @return
     */
    @Query("select r from RefundDetail  r where r.fkRefundId=?1")
    List<RefundDetail> getRefundDetailListByRefundId(BigDecimal refundId);

}
